package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.heatup.apt.api.service.CouponService;
import com.heatup.apt.api.service.UserService;
import com.heatup.apt.api.service.UserShareService;
import com.heatup.apt.common.util.ImageUtils;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;

/**
 * 用户邀请好友控制器
 * @author jixiangyun
 * 2016年11月26日 上午10:43:26
 */
@Controller
@RequestMapping("/user/share")
public class UserShareController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(UserShareController.class);
	@Autowired
	private UserShareService userShareService;
	
	@Autowired
	private CouponService couponService;
	
	@Autowired
	private UserService userService;
	/**
	 * 邀请好友
	 * @return
	 */
	@ResponseBody
	@RequestMapping("invite_friends")
	public String inviteFriends(HttpServletRequest request,HttpServletResponse response){
		String callback=RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel =new ApiResultModel();
		int activity_id = RequestTools.RequestInt(request, "activity_id",0);
		apiResultModel.setLogined(isLogined());
		apiResultModel.setData(userShareService.getMyCoupon(this.getUserId(),activity_id));
		apiResultModel.setMsg("显示邀请到的好友");
		// 
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 测试用途
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/testActiv")
	public String testActic(HttpServletRequest request,HttpServletResponse response){
		String callback=RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel =new ApiResultModel(isLogined());
		Object obj =  couponService.addCouponDetailByActivityId(12, this.getUserId());
		apiResultModel.setData(obj);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 分享活动链接时 查询可获得的优惠券
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/my_coupon_list")
	public String myCouponLlist(HttpServletRequest request,HttpServletResponse response){
		String callback=RequestTools.RequestString(request, "callback", "");
		int activity_id = RequestTools.RequestInt(request, "activity_id", 0);
		String isSelf = RequestTools.RequestString(request, "isSelf","true");
		ApiResultModel<Object> apiResultModel =new ApiResultModel();
		Map<String,Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("activity_id", activity_id);
		paramsMap.put("user_id",this.getUserInfo().getId());
		paramsMap.put("isSelf",isSelf);
		apiResultModel =  userShareService.queryMyCouponList(paramsMap);
		apiResultModel.setLogined(isLogined());
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	
	
	/**
	 * 分享图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/image")
	public String shareImage(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback=RequestTools.RequestString(request, "callback", "");
		Integer activityId = RequestTools.RequestInt(request, "activityId", 0);
		UserInfo userInfo = (UserInfo) this.getSession("USER_INFO");
		if(userInfo == null) {
			apiResultModel.setLogined(isLogined());
			apiResultModel.setData("未登录");
			apiResultModel.setErrorCode(ApiResultModel.RET_ERROR);
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		logger.info("userInfo value is : "+JSON.toJSONString(userInfo) );
		Map<String, Object> map = userService.getQRSceneImgUrl(userInfo.getId(), activityId);
		String base64Img = ImageUtils.imgUtils("1.png", map.get("imgUrl").toString(), userInfo.getNickName(), "10");
		apiResultModel.setData(base64Img);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
}
